package com.theobserver.theobserver;


import android.content.Intent;
import android.graphics.BitmapFactory;
import android.media.Image;
import android.support.annotation.NonNull;
import android.support.design.widget.NavigationView;
import android.support.design.widget.TabLayout;
import android.support.v4.app.FragmentManager;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewParent;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVFile;
import com.avos.avoscloud.AVObject;
import com.avos.avoscloud.AVUser;
import com.avos.avoscloud.GetCallback;
import com.avos.avoscloud.GetDataCallback;
import com.avos.avoscloud.LogUtil;

import org.w3c.dom.Text;

/**
 * Created by wssxk on 7/7/17.
 */

public class MainPageActivity extends FragmentActivity {

    NewsHotFragment hot_page = null;
    NewsXXPaperFragment xxpaper_page = null;
    NewsColumnFragment column_page = null;
    NewsSubscribFragment subscrib_page = null;
    NewsCollectFragment collect_page = null;

    TextView navi_name = null;
    TextView navi_email = null;
    ImageView navi_head = null;

    View.OnClickListener head_to_login = null;
    View.OnClickListener head_to_usercenter = null;

    public void onCreate(Bundle savedInstance)
    {
        super.onCreate(savedInstance);
        setContentView(R.layout.main_page_layout);
        /*
        ViewPager vp = (ViewPager) findViewById(R.id.viewpage_main_page_hot);
        FragmentManager fm = getSupportFragmentManager();
        MainPageHotNewsPageAdapter page_adapter = new MainPageHotNewsPageAdapter(fm);
        page_adapter.init(MainPageActivity.this);
        vp.setAdapter(page_adapter);
        TabLayout tl = (TabLayout) findViewById(R.id.tablayout_main_page_hot);
        tl.setTabMode(TabLayout.MODE_SCROLLABLE);
        //tl.setTabsFromPagerAdapter(page_adapter);
        tl.setupWithViewPager(vp);

        ImageView catalog_add = (ImageView) findViewById(R.id.imageview_catalog_select);
        catalog_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(MainPageActivity.this, CatalogSelectActivity.class));
            }
        });
        */

        NavigationView nv = (NavigationView) findViewById(R.id.main_page_navigation);
        View navi_headview = nv.getHeaderView(0);

        navi_name = navi_headview.findViewById(R.id.textview_navihead_name);
        navi_email = navi_headview.findViewById(R.id.textview_navihead_email);
        navi_head = navi_headview.findViewById(R.id.imageview_navihead_head);


        head_to_usercenter = new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivityForResult(new Intent(MainPageActivity.this, UserCenterActivity.class), Code.USERCENTERACTIVITY);
            }
        };

        head_to_login = new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivityForResult(new Intent(MainPageActivity.this, LogInActivity.class), Code.LOGINACTIVITY);
            }
        };


        final AVUser cur_user = AVUser.getCurrentUser();
        if(cur_user != null)
        {

            cur_user.fetchInBackground(new GetCallback<AVObject>() {
                @Override
                public void done(AVObject avObject, AVException e) {
                    if(e == null)
                    {
                        navi_name.setText(cur_user.getString("user_name"));
                        navi_email.setText(cur_user.getEmail());
                        AVFile user_head = cur_user.getAVFile("user_head");
                        navi_head.setOnClickListener(head_to_usercenter);
                        if(user_head != null)
                        {
                            user_head.getDataInBackground(new GetDataCallback() {
                                @Override
                                public void done(byte[] bytes, AVException e) {
                                    if(bytes != null)
                                    {
                                        navi_head.setImageBitmap(BitmapFactory.decodeByteArray(bytes, 0, bytes.length));
                                    }
                                    else if(e != null)
                                    {
                                        //LogUtil.log.d("MAINPAGE", e.toString());
                                        Toast.makeText(MainPageActivity.this, e.toString(), Toast.LENGTH_SHORT).show();
                                    }
                                }
                            });
                        }
                    }
                }
            });
            /*
            navi_name.setText(cur_user.getString("user_name"));
            navi_email.setText(cur_user.getEmail());
            AVFile user_head = cur_user.getAVFile("user_head");
            if(user_head != null)
            {
                user_head.getDataInBackground(new GetDataCallback() {
                    @Override
                    public void done(byte[] bytes, AVException e) {
                        if(bytes != null)
                        {
                            navi_head.setImageBitmap(BitmapFactory.decodeByteArray(bytes, 0, bytes.length));
                        }
                        else if(e != null)
                        {
                            //LogUtil.log.d("MAINPAGE", e.toString());
                            Toast.makeText(MainPageActivity.this, e.toString(), Toast.LENGTH_SHORT).show();
                        }
                    }
                });
            }
            */

        }
        else
        {
            navi_head.setOnClickListener(head_to_login);
        }


        final FragmentManager fm = getSupportFragmentManager();


        hot_page = new NewsHotFragment();
        hot_page.init(fm, MainPageActivity.this);
        xxpaper_page = new NewsXXPaperFragment();
        xxpaper_page.init(MainPageActivity.this);
        column_page = new NewsColumnFragment();
        column_page.init(MainPageActivity.this);
        subscrib_page = new NewsSubscribFragment();
        subscrib_page.init(this);
        collect_page = new NewsCollectFragment();
        collect_page.init(this);


        FragmentTransaction ft = fm.beginTransaction();


        ft.add(R.id.relativelayout_mainpage_content, hot_page);

        ft.commit();


        //NavigationView nv = (NavigationView) findViewById(R.id.main_page_navigation);
        Menu nv_menu = nv.getMenu();
        //nv_menu.getItem(R.id.menuitem_hot).setChecked(true);
        final MenuItem hot = nv_menu.getItem(0);
        hot.setChecked(true);

        //侧边栏页面切换
        nv.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {

                FragmentTransaction mFt = null;
                switch (item.getItemId())
                {
                    case R.id.menuitem_paper:
                        mFt = fm.beginTransaction();
                        mFt.replace(R.id.relativelayout_mainpage_content, xxpaper_page);
                        mFt.commit();
                        break;
                    case R.id.menuitem_hot:
                        mFt = fm.beginTransaction();
                        mFt.replace(R.id.relativelayout_mainpage_content, hot_page);
                        mFt.commit();
                        break;
                    case R.id.menuitem_column:
                        mFt = fm.beginTransaction();
                        mFt.replace(R.id.relativelayout_mainpage_content, column_page);
                        mFt.commit();
                        break;
                    case R.id.menuitem_subscrib:
                        /*
                        if(AVUser.getCurrentUser() == null)
                        {
                            Toast.makeText(MainPageActivity.this, "请先登陆", Toast.LENGTH_SHORT).show();
                        }
                        else {
                            mFt = fm.beginTransaction();
                            mFt.replace(R.id.relativelayout_mainpage_content, subscrib_page);
                            mFt.commit();
                        }
                        */
                        mFt = fm.beginTransaction();
                        mFt.replace(R.id.relativelayout_mainpage_content, subscrib_page);
                        mFt.commit();
                        break;
                    case R.id.menuitem_collect:
                        /*
                        if(AVUser.getCurrentUser() == null)
                        {
                            Toast.makeText(MainPageActivity.this, "请先登陆", Toast.LENGTH_SHORT).show();
                        }
                        else {
                            mFt = fm.beginTransaction();
                            mFt.replace(R.id.relativelayout_mainpage_content, collect_page);
                            mFt.commit();
                        }
                        */
                        mFt = fm.beginTransaction();
                        mFt.replace(R.id.relativelayout_mainpage_content, collect_page);
                        mFt.commit();
                        break;
                    case R.id.menuitem_usercenter:
                        //用户中心的有关转跳
                        if(AVUser.getCurrentUser() == null)
                        {
                            startActivityForResult(new Intent(MainPageActivity.this, LogInActivity.class), Code.LOGINACTIVITY);
                        }
                        else
                        {
                            startActivityForResult(new Intent(MainPageActivity.this, UserCenterActivity.class), Code.USERCENTERACTIVITY);
                        }

                        //startActivity(new Intent(MainPageActivity.this, NewsSendedActivity.class));
                        break;
                    case R.id.menu_setting:
                        //转跳进入设置
                        startActivity(new Intent(MainPageActivity.this, SettingActivity.class));
                        break;
                    case R.id.menu_quit:
                        finish();
                        break;


                }

                return true;
            }
        });






    }

    public void onActivityResult(int requestCode,int ResuleCode,Intent data)
    {
        switch (requestCode)
        {
            //从用户中心或登陆页返回时刷新用户信息
            case Code.USERCENTERACTIVITY:
            case Code.LOGINACTIVITY:
                AVUser cur_user = AVUser.getCurrentUser();
                if(cur_user != null)
                {
                    navi_name.setText(cur_user.getString("user_name"));
                    navi_email.setText(cur_user.getEmail());
                    AVFile user_head = cur_user.getAVFile("user_head");
                    navi_head.setOnClickListener(head_to_usercenter);
                    if(user_head != null)
                    {
                        user_head.getDataInBackground(new GetDataCallback() {
                            @Override
                            public void done(byte[] bytes, AVException e) {
                                if(bytes != null)
                                {
                                    navi_head.setImageBitmap(BitmapFactory.decodeByteArray(bytes, 0, bytes.length));
                                }
                            }
                        });
                    }

                }
                else
                {
                    navi_name.setText("未登录");
                    navi_email.setText("");
                    navi_head.setImageResource(R.drawable.head_default);
                    navi_head.setOnClickListener(head_to_login);
                }
                break;
        }
    }
}
